#include "HeroAttrCacheData.h"


HeroAttrCacheData* HeroAttrCacheData::mInstance = NULL;

void HeroAttrCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new HeroAttrCacheData();
	}
}

HeroAttrCacheData* HeroAttrCacheData::GetInstance()
{
	return mInstance;
}

HeroAttrCacheData::HeroAttrCacheData()
	: m_maxCount(0)
{
}

HeroAttrCacheData::~HeroAttrCacheData()
{
	Clear();
}

bool HeroAttrCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void HeroAttrCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool HeroAttrCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/hero/heroAttr.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/hero/heroAttr.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}

void HeroAttrCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		HeroAttr* p = new HeroAttr;
		_safe_check(p);

		p->mId = jm["heros_id"].asUInt();
		strcpy(p->mName, jm["name"].asString().c_str());
		p->mHeroId = jm["hero_id"].asUInt();
		p->mGrade = jm["grade"].asUInt();
		p->mNextGrade = jm["next_grade"].asUInt();
		p->mAnger = jm["anger"].asUInt();

		Json::Value equipment = jm["equipment"];
		for (int i = 0; i < 6; ++i)
		{
			p->mEquipment[i] = equipment[i].asUInt();
		}

		p->mPower = jm["power"].asDouble();
		p->mIntelligence = jm["intelligence"].asDouble();
		p->mAgility = jm["agility"].asDouble();
		p->mHp = jm["hp"].asDouble();
		p->mAttack = jm["attack"].asDouble();
		p->mArmor = jm["armor"].asDouble();
		p->mMagicResist = jm["magic_resist"].asDouble();
		p->mCritical = jm["critical"].asDouble();
		p->mDodge = jm["dodge"].asDouble();
		p->mHitRatio = jm["hit_ratio"].asDouble();
		p->mArmorPenetration = jm["armor_penetration"].asDouble();
		p->mMagicPenetration = jm["magic_penetration"].asDouble();
		p->mSuckBlood = jm["suck_blood"].asDouble();
		p->mTenacity = jm["tenacity"].asDouble();
		p->mMovementSpeed = jm["movement_speed"].asDouble();
		p->mAttackSpeed = jm["attack_speed"].asDouble();
		p->mStrikingDistance = jm["striking_distance"].asDouble();

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

HeroAttr* HeroAttrCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool HeroAttrCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	HeroAttr* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error6");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);
	_jm["name"] = Json::Value(p->mName);
	_jm["hero_id"] = Json::Value(p->mHeroId);
	_jm["grade"] = Json::Value(p->mGrade);
	_jm["next_grade"] = Json::Value(p->mNextGrade);
	_jm["anger"] = Json::Value(p->mAnger);

	Json::Value equipment;
	for (int i = 0; i < 6; ++i)
	{
		if ( p->mEquipment[i] )
			equipment.append(p->mEquipment[i]);
	}
	_jm["equipment"] = equipment;

	_jm["power"] = Json::Value(p->mPower);
	_jm["intelligence"] = Json::Value(p->mIntelligence);
	_jm["agility"] = Json::Value(p->mAnger);
	_jm["hp"] = Json::Value(p->mHp);
	_jm["attack"] = Json::Value(p->mAttack);
	_jm["armor"] = Json::Value(p->mArmor);
	_jm["magic_resist"] = Json::Value(p->mMagicResist);
	_jm["critical"] = Json::Value(p->mCritical);
	_jm["dodge"] = Json::Value(p->mDodge);
	_jm["hit_ratio"] = Json::Value(p->mHitRatio);
	_jm["armor_penetration"] = Json::Value(p->mArmorPenetration);
	_jm["magic_penetration"] = Json::Value(p->mMagicPenetration);
	_jm["suck_blood"] = Json::Value(p->mSuckBlood);
	_jm["tenacity"] = Json::Value(p->mTenacity);
	_jm["movement_speed"] = Json::Value(p->mMovementSpeed);
	_jm["attack_speed"] = Json::Value(p->mAttackSpeed);
	_jm["striking_distance"] = Json::Value(p->mStrikingDistance);

	_safe_guard_end;

	return true;
}

uint32_t HeroAttrCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}